package edu.asu.sefcom.extract.profile;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;

public class ProfileFollowsParser1 extends MultiFieldParser {

	Logger logger = Logger.getLogger(ProfileFollowsParser1.class);

	@Override
	public List<String> extractFields(String post) {
		String start = "Friends</a>:";
		String stop = "title=\"Communities\"";
		
		String targetStr="";
		try{
				targetStr = StringUtil.returnNormalPost(
						start, stop, post, 0);
		}catch(Exception e){
			logger.error(e);
		}
		try{
			if(StringUtil.isBlank(targetStr)){
				stop = "<td style=\"text-align: left; font-weight: bold;\" nowrap=\"nowrap\"";			
			targetStr = StringUtil.returnNormalPost(
					start, stop, post, 0);
			}
		}catch(Exception e){logger.error(e);}
		
		try{
			if(StringUtil.isBlank(targetStr)){
			stop = "Member of:";
			targetStr = StringUtil.returnNormalPost(
					start, stop, post, 0);
			}
		}catch(Exception e){logger.error(e);}
		
		String regex = "<a href=\"http://.*livejournal.com/profile\" rel=\"friend\">.*</a>";
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(targetStr);
		List<String> communities = new ArrayList<String>();
		int counter = 0;
		String txt = "";
		if (m.find()) {
			txt = m.group(counter++);
			txt = Jsoup.parse(txt).text();
		}
		communities = Arrays.asList(txt.split(","));
		logger.warn("follows : " + communities);
		return communities;
	}
}
